package util.cleaner;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

import util.io.FileInput;

public class EnrichGraph {
	static private int _id_url = 0;
	static private int _id_tag = 1;
	static private int _url = 2;
	static private int _tag = 3;
	static private int _freq_u = 5;
	static private int _freq_tag = 4;
	static private int _freq_ut = 6;
	static private int _age = 7;
	

	int offset= 10000000;
	
	public static void trimByFreq(String path) throws MalformedURLException{
		
		FileInput in = new FileInput(path);
		String line = in.readString();
		int ids = 0;

		Hashtable<String, Integer> urls_h = new Hashtable<String, Integer>();
		Hashtable<String,Hashtable<String,Integer>> links_adults = new Hashtable<String,Hashtable<String,Integer>>();
		Hashtable<String, Integer> tags_h = new Hashtable<String, Integer>();
		Hashtable<String, Integer> domains_h = new Hashtable<String, Integer>();

		// add url ids

		while (line != null) {

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer
					.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer
					.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer
					.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			String domain = extractDomain(url_name);
			if ( age.contains("adults")) {

				tags_h.put(name, freq_tag);
				

				if(urls_h.containsKey(url_name)){
				
					//add tag 
					Hashtable<String, Integer> temp = links_adults.get(url_name);
					if(!temp.containsKey(name)){
						temp.put(name, freq_ut);
						links_adults.put(url_name, temp);
				
					}
				}else{
					
					//add link
					
					Hashtable<String, Integer> temp = new Hashtable<String,Integer>();
					temp.put(name, freq_ut);
					links_adults.put(url_name,temp);
					
					urls_h.put(url_name, freq_url);
					
					if(domains_h.containsKey(domain)){
						 
						domains_h.put(domain, domains_h.get(domain)+ freq_url);
					}
					
					else{
						
					domains_h.put(domain, freq_url);
					}
				}
	
				

			}

			line = in.readString();
		}
		
		
		//regerate file
		
		in = new FileInput(path);
		line= in.readString();
	
		while(line!=null){

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			String domain = extractDomain(url_name);
			

			int tag_f_a=0;
			int url_f_a=0;
			int domain_f_a=0;
			
			int link_f_a =0;
			
			
			
			if(tags_h.containsKey(name)){
				tag_f_a= tags_h.get(name);
			}
			
			if(urls_h.containsKey(url_name)){
				url_f_a= urls_h.get(url_name);
				Hashtable<String, Integer> temp = links_adults.get(url_name);
				if(temp.containsKey(name)){
					
					link_f_a= temp.get(name);
				}			
			}
			
			if(domains_h.containsKey(domain)){
				
				domain_f_a= domains_h.get(domain);
			}
			
			System.out.println(id_url + "\t"+ id_tag + "\t"+ url_name + "\t"+ name +"\t"
					+freq_tag + "\t"+ freq_url + "\t"+ freq_ut + "\t"+ age + "\t"+ tag_f_a + "\t"+ url_f_a + "\t"+ domain_f_a + "\t"+link_f_a );
			
			
			line=in.readString();
			
		}
			
			
		
		
		
		
	}
	
	public static void main(String argsp[]) throws MalformedURLException{
		
		String path_gold ="/Users/sergioduarte/projects/data/query_suggestions/query_suggestions/query_suggestions.txt";
		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz_revised.txt";
		
		trimByFreq(path);
	}
	
	public static String extractDomain(String url) throws MalformedURLException{
		
		URL a = new URL(url);
		String host= a.getHost();
		host= host.replace("www.", "");
		return host;
	}
	
}
